﻿using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Runtime.InteropServices;
using System.Text;
using System.Threading;
using System.Windows;
using GEPSDevelopEnvironment.FileDialog;
using Microsoft.Win32;

namespace GEPSDevelopEnvironment.Common
{
    internal static class FileCommonUtils
    {
        private static IntPtr _Window;
        private static string _Title;
        private static bool EnumWindowsProc(IntPtr hWnd, IntPtr lParam)
        {
            _Window = IntPtr.Zero;
            if (Win32_API.GetParent(hWnd) == lParam)
            {
                _Window = hWnd;
                return false;
            }
            return true;
        }

        //private static void AdjustEditBox()
        //{
        //    while (true)
        //    {
        //        Win32_API.EnumWindowsProc oEnumWindowsProc = EnumWindowsProc;
        //        Win32_API.EnumChildWindows(Win32_API.GetDesktopWindow(), oEnumWindowsProc, 
        //            Process.GetCurrentProcess().MainWindowHandle);
        //        _Window = Win32_API.FindWindowEx(_Window, IntPtr.Zero, "DUIViewWndClassName", "");
        //        _Window = Win32_API.FindWindowEx(_Window, IntPtr.Zero, "DirectUIHWND", "");
        //        _Window = Win32_API.FindWindowEx(_Window, IntPtr.Zero, "FloatNotifySink", "");
        //        if (_Window != IntPtr.Zero)
        //        {
        //            Win32_API.SetWindowPos(_Window, IntPtr.Zero, 175, 330, 510, 30, SetWindowPosFlags.ShowWindow);
        //        }
        //        _Window = Win32_API.FindWindowEx(_Window, IntPtr.Zero, "Edit", "");
        //        if(_Window != IntPtr.Zero)
        //        {
        //            Win32_API.SetWindowPos(_Window, IntPtr.Zero, 0, 0, 510, 30, SetWindowPosFlags.ShowWindow);
        //            string strTitle = Win32_API.GetWindowTextRaw(_Window);
        //            if (strTitle != "")
        //            {
        //                _Title = strTitle;
        //            }
        //        }
        //    }
        //}

        public static string SelectPath(string AInitDir="")
        {
            IFileOpenDialog iFolderSelector = new WinAPI_FileOpenDialog() as IFileOpenDialog;
            iFolderSelector.SetOptions(FOS.FOS_PICKFOLDERS);
            //IFileDialogCustomize iCustomize = (IFileDialogCustomize) iFolderSelector;
            //iCustomize.StartVisualGroup(10000, "自定义路径：");
            //iCustomize.AddEditBox(10001, "");
            //iCustomize.EndVisualGroup();

            Guid guid = new Guid(ShellIIDGuid.IShellItem2);
            IShellItem2 defaultDirectoryShellItem;
            int ret = ShellNativeMethods.SHCreateItemFromParsingName(AInitDir, IntPtr.Zero, ref guid,
                                                                        out defaultDirectoryShellItem);
            iFolderSelector.SetDefaultFolder(defaultDirectoryShellItem);
            iFolderSelector.SetFolder(defaultDirectoryShellItem);
            string sFileName = AInitDir;
            //Thread oThread = new Thread(AdjustEditBox);
            //oThread.Start();
            ret = iFolderSelector.Show(Process.GetCurrentProcess().MainWindowHandle);
            //if (ret == 0)
            //{
                //ret = iCustomize.GetEditBoxText(10001, out sFileName);
                //if (sFileName == "")
                //{
                    IShellItem iShellResult;
                    
                    try
                    {
                        iFolderSelector.GetResult(out iShellResult);
                        iShellResult.GetDisplayName(SIGDN.FILESYSPATH, out sFileName);
                    }
                    catch (Exception e)
                    {
                        sFileName = AInitDir;
                    }
                //}
            //}
            return sFileName;
        }

        public static string SelectFile(string AFilter, string AInitDir="")
        {
            OpenFileDialog oOpenFile = new OpenFileDialog();
            oOpenFile.Filter = AFilter;
            oOpenFile.InitialDirectory = AInitDir;
            oOpenFile.ShowDialog();
            return oOpenFile.FileName;
        }

        public static string SaveFile(string AFilter, string AInitDir = "")
        {
            SaveFileDialog oSaveFile = new SaveFileDialog();
            oSaveFile.Filter = AFilter;
            oSaveFile.InitialDirectory = AInitDir;
            oSaveFile.ShowDialog();
            return oSaveFile.FileName;
        }

        public static void MappingPath(string AJunction, string ASrcPath, string ADstPath)
        {
            
        }

        public static void CopyFolder(string ASrcPath, string ADstPath)
        {
            if (!Directory.Exists(ASrcPath))
            {
                return;
            }
            Directory.CreateDirectory(ADstPath);
            string[] files = Directory.GetFiles(ASrcPath);
            foreach (string file in files)
            {
                string name = Path.GetFileName(file);
                string dest = Path.Combine(ADstPath, name);
                File.Copy(file, dest);
            }
            string[] folders = Directory.GetDirectories(ASrcPath);
            foreach (string folder in folders)
            {
                string name = Path.GetFileName(folder);
                string dest = Path.Combine(ADstPath, name);
                CopyFolder(folder, dest);
            }
        }

        public static void DeleteDirectory(string APath)
        {
            if (Directory.Exists(APath))
            {
                Directory.Delete(APath, true);
            }
        }
    }
}
